package bb.lanxing.model.data;

import android.os.Parcel;
import android.os.Parcelable;

import com.baidu.mapapi.model.LatLng;
import com.github.mikephil.charting.utils.Utils;
import com.umeng.socialize.net.utils.SocializeProtocolConstants;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import bb.lanxing.util.JsonUtil;

public class Notepoint implements IBasePOI, Parcelable {
    public static final Creator<Notepoint> CREATOR = new Creator<Notepoint>() {
        @Override
        public Notepoint[] newArray(int i) {
            return new Notepoint[i];
        }

        @Override
        public Notepoint createFromParcel(Parcel parcel) {
            return new Notepoint(parcel);
        }
    };
    private String content;
    private String image;
    private double latitude;
    private double longitude;
    private String title;

    @Override
    public int describeContents() {
        return 0;
    }

    public Notepoint() {
    }

    public Notepoint(JSONObject jSONObject) {
        this.latitude = JsonUtil.getDoubleValue("latitude", jSONObject);
        this.longitude = JsonUtil.getDoubleValue("longitude", jSONObject);
        this.title = JsonUtil.getStringValue("title", jSONObject);
        this.content = JsonUtil.getStringValue("content", jSONObject);
        this.image = JsonUtil.getStringValue(SocializeProtocolConstants.IMAGE, jSONObject);
        if (this.latitude == Utils.DOUBLE_EPSILON) {
            this.latitude = JsonUtil.getDoubleValue("lat", jSONObject);
        }
        if (this.longitude == Utils.DOUBLE_EPSILON) {
            this.longitude = JsonUtil.getDoubleValue("lng", jSONObject);
        }
    }

    public Notepoint(Parcel parcel) {
        this.latitude = parcel.readDouble();
        this.longitude = parcel.readDouble();
        this.title = parcel.readString();
        this.content = parcel.readString();
        this.image = parcel.readString();
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeDouble(this.latitude);
        parcel.writeDouble(this.longitude);
        parcel.writeString(this.title);
        parcel.writeString(this.content);
        parcel.writeString(this.image);
    }

    public JSONObject toJson() {
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("lat", this.latitude);
            jSONObject.put("lng", this.longitude);
            jSONObject.put("title", this.title);
            jSONObject.put("content", this.content);
            jSONObject.put(SocializeProtocolConstants.IMAGE, this.image);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jSONObject;
    }

    public static JSONArray toJsonArray(List<Notepoint> list) {
        JSONArray jSONArray = new JSONArray();
        for (Notepoint notepoint : list) {
            LatLng latLng = notepoint.getLatLng();
            JSONObject jSONObject = new JSONObject();
            try {
                jSONObject.put("lat", latLng.latitude);
                jSONObject.put("lng", latLng.longitude);
                jSONObject.put("title", notepoint.getTitle());
                jSONObject.put("content", notepoint.getContent());
                jSONObject.put(SocializeProtocolConstants.IMAGE, notepoint.getImage());
                jSONArray.put(jSONObject);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return jSONArray;
    }

    @Override
    public String getImage() {
        return this.image;
    }

    @Override
    public void setImage(String str) {
        this.image = str;
    }

    @Override
    public String getContent() {
        return this.content;
    }

    @Override
    public void setContent(String str) {
        this.content = str;
    }

    @Override
    public String getTitle() {
        return this.title;
    }

    @Override
    public void setTitle(String str) {
        this.title = str;
    }

    @Override
    public double getLongitude() {
        return this.longitude;
    }

    @Override
    public void setLongitude(double d) {
        this.longitude = d;
    }

    @Override
    public double getLatitude() {
        return this.latitude;
    }

    @Override
    public void setLatitude(double d) {
        this.latitude = d;
    }

    @Override
    public void setLatLng(LatLng latLng) {
        this.latitude = latLng.latitude;
        this.longitude = latLng.longitude;
    }

    @Override
    public LatLng getLatLng() {
        return new LatLng(this.latitude, this.longitude);
    }
}
